<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Commerce\Repository\Commerce_energy;

use Plugin\Attractions\Commerce\Model\Commerce_energy\CommerceEnergy;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 商户能耗 Repository类
 */
class CommerceEnergyRepository extends IRepository
{
   public function __construct(
        protected readonly CommerceEnergy $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 主键ID
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 商户ID
        if (isset($params['merchant_id']) && !empty($params['merchant_id'])) {
            $query->where('merchant_id', '=', $params['merchant_id']);
        }

        // 能耗月份（格式：2024-01）
        if (isset($params['consumption_month']) && !empty($params['consumption_month'])) {
            $query->where('consumption_month', 'like', '%'.$params['consumption_month'].'%');
        }

        // 费用类型：1-水费，2-电费，3-气费，4-其他
        if (isset($params['cost_type']) && !empty($params['cost_type'])) {
            $query->where('cost_type', '=', $params['cost_type']);
        }

        // 用量
        if (isset($params['cost_usage']) && !empty($params['cost_usage'])) {
            $query->where('cost_usage', '=', $params['cost_usage']);
        }

        // 单价
        if (isset($params['cost_price']) && !empty($params['cost_price'])) {
            $query->where('cost_price', '=', $params['cost_price']);
        }

        // 表读数
        if (isset($params['cost_meter_reading']) && !empty($params['cost_meter_reading'])) {
            $query->where('cost_meter_reading', '=', $params['cost_meter_reading']);
        }

        // 总费用
        if (isset($params['total_amount']) && !empty($params['total_amount'])) {
            $query->where('total_amount', '=', $params['total_amount']);
        }

        // 缴费状态：1-未缴费，2-已缴费
        if (isset($params['payment_status']) && !empty($params['payment_status'])) {
            $query->where('payment_status', '=', $params['payment_status']);
        }

        // 缴费日期
        if (isset($params['payment_date']) && !empty($params['payment_date']) && is_array($params['payment_date']) && count($params['payment_date']) == 2) {
            $query->whereBetween(
                'payment_date',
                [ $params['payment_date'][0], $params['payment_date'][1] ]
            );
        }

        // 付款方式1-银行转账,2-微信支付,3-支付宝支付,4-现金,5-其他付款方式
        if (isset($params['payment_method']) && !empty($params['payment_method'])) {
            $query->where('payment_method', '=', $params['payment_method']);
        }

        // 发票号
        if (isset($params['invoice_id']) && !empty($params['invoice_id'])) {
            $query->where('invoice_id', 'like', '%'.$params['invoice_id'].'%');
        }

        // 抄表照片
        if (isset($params['meter_photos']) && !empty($params['meter_photos'])) {
            $query->where('meter_photos', 'like', '%'.$params['meter_photos'].'%');
        }

        // 账单照片
        if (isset($params['bills_photos']) && !empty($params['bills_photos'])) {
            $query->where('bills_photos', 'like', '%'.$params['bills_photos'].'%');
        }

        // 备注
        if (isset($params['remark']) && !empty($params['remark'])) {
            $query->where('remark', '=', $params['remark']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 
        if (isset($params['deleted_at']) && !empty($params['deleted_at']) && is_array($params['deleted_at']) && count($params['deleted_at']) == 2) {
            $query->whereBetween(
                'deleted_at',
                [ $params['deleted_at'][0], $params['deleted_at'][1] ]
            );
        }

        return $query;
    }
}